03. Exercise: A Basic Notification

L1 A03 A Basic Notifcation V2

Android Developer Documentation

Exercise

  1. Open the NotificationUtils.kt class and find Step 1.1. You will be implementing an extension function to NotificationManager to send notifications without rewriting the necessary code. The empty sendNotification() function is already given and has TODOs where you will add the implementation.
// TODO: Step 1.1 extension function to send messages (GIVEN)
/**
 * Builds and delivers a notification.
 *
 * @param messageBody, notification text.
 * @param context, activity context.
 */
fun NotificationManager.sendNotification(messageBody: String, applicationContext: Context) {
  1. To support devices running older versions you need to use NotificationCompat builder instead of notification builder. Get an instance of the NotificationCompat builder, pass in the app context and a channel id. The channel id is a string value from string resources which uses the matching channel. Starting with API level 26, all notifications must be assigned to a channel. We're going to go into channels in much more detail later - for now, just use the string channel id.
// TODO: Step 1.2 get an instance of NotificationCompat.Builder
val builder = NotificationCompat.Builder(
        applicationContext,
        applicationContext.getString(R.string.egg_notification_channel_id)
)
  1. Set the notification icon to represent your app, title and the content text for the message you want to give to the user. You'll see more options to customize your notification further in the lesson but this is the minimum amount of data you need to set in order to send a notification.
       // TODO: Step 1.3 set title, text and icon to builder
        .setSmallIcon(R.drawable.cooked_egg)
        .setContentTitle(applicationContext
        .getString(R.string.notification_title))
        .setContentText(messageBody)
  1. Next, you need to call notify() with a unique id for your notification and with the Notification object from your builder. This id represents the current notification instance and is needed for updating or canceling this notification. Since your app will only have one active notification at a given time, you can use the same id for all your notifications. You are already given a constant for this purpose called NOTIFICATION_ID in NotificationUtils.kt. Notice we can directly call notify() since you are performing the call from an extension function on the same class.
  // TODO: Step 1.4 call notify to send the notification
  // Deliver the notification
  notify(NOTIFICATION_ID, builder.build())
  1. Now that we've written all the code we need to create and trigger a notification in this extension function - let's call it. Open EggTimerViewModel.kt and find the startTimer() function. This function creates an alarm with the selected time interval when the user enables your egg timer.

  2. Next let’s trigger a notification in this function when the user starts the timer. In order to call the sendNotification() function you previously implemented, you need an instance of NotificationManager. NotificationManager is a system service which provides all the functions exposed for notifications api, including the extension function you added. Anytime you want to send, cancel or update a notification you need to request an instance of the NotificationManager from the system. Call the sendNotification() extension function with the notification message and with the context.

// EggTimerViewModel.kt
// TODO: Step 1.5 get an instance of NotificationManager 
// and call sendNotification

val notificationManager = ContextCompat.getSystemService(
    app, 
    NotificationManager::class.java
) as NotificationManager
notificationManager.sendNotification(app.getString(R.string.timer_running), app)

If you open logcatand search for “No Channel found”, you should see an error message that the egg_channel, you are trying to use, does not exist. In the following steps you will learn more about the Notification Channels and how to create one.